package com.group3.merchant.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import javax.jms.JMSException;

import org.apache.activemq.selector.ParseException;
import org.apache.commons.codec.binary.Base64;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.group3.entity.Merchant;
import com.group3.merchant.dao.IMerchantDao;
import com.group3.merchant.dao.impl.MerchantDaoImpl;
import com.group3.merchant.service.IMerchantService;
import com.group3.merchant.utils.HttpUtil;
import com.group3.merchant.utils.JMSUtil;
import com.group3.merchant.utils.JsonUtil;

public class MerchantServiceImpl implements IMerchantService {
	private IMerchantDao dao = new MerchantDaoImpl();

	@Override
	public Merchant register(Merchant merchant) {
		Merchant merch = dao.insertMerchant(merchant);

		return jmsService(merch);
		
	}

	@Override
	public Merchant login(String name, String password) {
		return dao.login(name, password);
	}

	@Override
	public List<String> getStatus(Merchant merchant) {
		// Http
		List<String> tlist = null;
		String jsonStr = null;
		try {
			jsonStr = HttpUtil.reqLogin(merchant.getmId());
			if(jsonStr == null) {
				return null;
			}
			tlist = JsonUtil.getInstance().getObjectFromJson(jsonStr,
					List.class);
			System.out.println(tlist);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return tlist;
	}

	@Override
	public Merchant motify(Merchant merchant) {
		Merchant merch = dao.update(merchant);
		Merchant merch2 = jmsService(merch);
		return merch2;
	}

	private Merchant jmsService(Merchant merchant) {
		Merchant merchant2 = new Merchant(merchant.getmId(), merchant.getmName(), merchant.getPassword(), merchant.getAddress(), merchant.getImageUrl(), merchant.getIdCardUrl());
		File imgFile = new File(merchant.getImageUrl());
		File icFile = new File(merchant.getIdCardUrl());

		Base64 base64 = new Base64();
		FileInputStream inputFile1 = null;
		FileInputStream inputFile2 = null;
		try {
			inputFile1 = new FileInputStream(imgFile);
			inputFile2 = new FileInputStream(icFile);
		} catch (FileNotFoundException e3) {
			e3.printStackTrace();
		}
		byte[] imgbuffer = new byte[(int) imgFile.length()];
		byte[] icbuffer = new byte[(int) icFile.length()];
		try {
			inputFile1.read(imgbuffer);
			inputFile2.read(icbuffer);
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}

		merchant.setImageUrl(base64.encodeAsString(imgbuffer));
		merchant.setIdCardUrl(base64.encodeAsString(icbuffer));

		ObjectMapper mapper = new ObjectMapper();
		String jsonstr = null;
		try {
			jsonstr = mapper.writeValueAsString(merchant);
			JMSUtil.send(jsonstr);
		} catch (JsonProcessingException e1) {
			e1.printStackTrace();
		} catch (JMSException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		try {
			inputFile1.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return merchant2;
	}

}
